<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Widgetmodel extends CI_Model {

	protected $_CI;

	public static $searchField = array(
		'project_id' => 'project_id',
		'widget_id' => 'widget_id',
	);
	public static $likeField = array(
		'widget_name' => 'widget_name',
	);

	public static $sortField = array(
		'widget_id' => 'widget_id',
		'widget_time_lastmodified' => 'widget_time_lastmodified',
	);

	public function __construct(){
		parent::__construct();
		$this->_CI = & get_instance();
	}
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
	public function getWidget($widget_id){
		$this->db->from('widget');
		$this->db->where('widget_id',$widget_id);
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item;
	}
	
	public function getWidgetListSection($project_ids,$assist){
		$this->db->select('widget_id,project_id,user_id,widget_name,widget_time_lastmodified');
		$this->db->from('widget');
		$this->db->where('widget_status !=',STATUS_DELETE);
		$this->db->where_in('project_id',$project_ids);
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		if(!empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
		
		$count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

		$this->db->order_by('widget_time_lastmodified','desc');
		if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
		$query = $this->db->get();
		$list = $query->result_array();

		return array($list,$count);
	}


	public function getWidgetBasicListByProject($project_id,$assist){
		$this->db->select('widget_id,widget_name');
		$this->db->from('widget');
		$this->db->where('project_id',$project_id);
		$this->db->where('widget_status !=',STATUS_DELETE);
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		$this->db->order_by('widget_name','asc');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getWidgetListByStruct($struct,$project_id){

		preg_match_all("/<widget>(.*)<\/widget>/", $struct, $matches);
		if(strlen($struct)<15||!count($matches[1])) return array();
		$this->db->_protect_identifiers = FALSE;
		$this->db->from('widget');
		$this->db->where('project_id',$project_id);
		$this->db->where('widget_status !=',STATUS_DELETE);
		$this->db->where_in('widget_name',$matches[1]);
		$this ->db->order_by('FIELD(widget_name,\''.implode('\',\'',$matches[1]).'\')');
		$query = $this->db->get();
		$list = $query->result_array();
		$this->db->_protect_identifiers = TRUE;

		return $list;
	}

	public function getWidgetListByName($widget_names,$project_id){
		if(!is_array($widget_names)) $widget_names = array($widget_names);
		if(empty($widget_names)) $widget_names[] = -1;

		$this->db->select('widget_id,widget_name');
		$this->db->from('widget');
		$this->db->where_in('widget_name',$widget_names);
		$this->db->where('project_id',$project_id);
		$this->db->where('widget_status !=',STATUS_DELETE);
		$this->db->order_by('widget_id');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getWidgetListByProject($project_id){
		$this->db->from('widget');
		$this->db->where('project_id',$project_id);
		$this->db->where('widget_status !=',STATUS_DELETE);
		$this->db->order_by('widget_id','desc');
		$query = $this->db->get();
		$list = $query->result_array();

		return $list;
	}

	public function getWidgetTemplateListSection($widget_name,$project_id,$assist){
		$this->db->select('template_id,project_id,user_id,template_name,template_time_lastmodified');
		$this->db->from('template');
		$this->db->where('project_id',$project_id);
		$this->db->like('template_struct','<widget>'.$widget_name.'</widget>');
		if(!empty($assist['where'])) $this->db->where($assist['where']);
		if(!empty($assist['like'])) $this->db->like($assist['like']);
		$count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

		$this->db->order_by('template_time_lastmodified','desc');
		if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
		$query = $this->db->get();
		$list = $query->result_array();

		return array($list,$count);
	}

	public function getWidgetTemplate($widget_name,$project_id){
		$this->db->select('template_id');
		$this->db->from('template');
		$this->db->where('project_id',$project_id);
		$this->db->like('template_struct','<widget>'.$widget_name.'</widget>');
		$this->db->limit(1);
		$query = $this->db->get();
		$item = $query->row_array();

		return $item['template_id'];
	}
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
	public function createWidget($info){
		if(empty($info['widget_css'])) {
			$info['widget_css'] = '';
		}
		if(empty($info['widget_js'])) {
			$info['widget_js'] = '';
		}
		if(empty($info['widget_html'])) {
			$info['widget_html'] = '';
		}
		$this->db->insert('widget',$info);
		return $this->db->insert_id();
	}

	public function createWidgetHistory($info){
		if(empty($info['widget_history_css'])) {
			$info['widget_history_css'] = '';
		}
		if(empty($info['widget_history_js'])) {
			$info['widget_history_js'] = '';
		}
		if(empty($info['widget_history_html'])) {
			$info['widget_history_html'] = '';
		}
		$this->db->insert('widget_history',$info);
		return $this->db->insert_id();
	}
/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
	public function editWidget($widget_id,$info){
		if(empty($info['widget_css'])) {
			$info['widget_css'] = '';
		}
		if(empty($info['widget_js'])) {
			$info['widget_js'] = '';
		}
		if(empty($info['widget_html'])) {
			$info['widget_html'] = '';
		}
		$this->db->where('widget_id',$widget_id);
		$this->db->update('widget',$info);
	}
/*
| -------------------------------------------------------------------
|  CHECK Functions
| -------------------------------------------------------------------
*/
	public function checkNameAvailable($name,$project_id,$widget_id = 0){
		$this->db->from('widget');
		$this->db->where('widget_id !=',$widget_id);
		$this->db->where('widget_status !=',STATUS_DELETE);
		$this->db->where('project_id',$project_id);
		$this->db->where('widget_name',$name);
		$count = $this->db->count_all_results();

		return ($count == 0);
	}
}